package com.woshengqile.service;

import java.util.List;

import com.woshengqile.dao.VideoInfoCategoryDao;
import com.woshengqile.model.VideoInfoCategory;
import com.woshengqile.util.Assert;

import net.hasor.core.Inject;

public class VideoInfoCategoryService extends BaseService<VideoInfoCategory> {
	
	@Inject
	private VideoInfoCategoryDao videoCategoryDao;

	public VideoInfoCategoryService() {
		super(VideoInfoCategory.class);
	}
	
	@Override
	public VideoInfoCategory save(VideoInfoCategory videoCategory) {
		Assert.notNull(videoCategory);

		setValue(videoCategory);
		return super.save(videoCategory);
	}
	
	/**
	 * 查找顶级视频分类
	 * @param count 返回数量
	 * @return
	 */
	public List<VideoInfoCategory> findRoots(Integer count) {
		return videoCategoryDao.findRoots(count);
	}
	
	/**
	 * 设置值
	 * @param videoCategory 视频分类
	 */
	private void setValue(VideoInfoCategory videoCategory) {
		if (videoCategory == null) {
			return;
		}
		VideoInfoCategory parent = videoCategory.getParent();
		if (parent != null) {
			videoCategory.setTreePath(parent.getTreePath() + parent.getId() + VideoInfoCategory.TREE_PATH_SEPARATOR);
			videoCategory.setParentId(parent.getId());
		} else {
			videoCategory.setTreePath(VideoInfoCategory.TREE_PATH_SEPARATOR);
		}
		videoCategory.setGrade(videoCategory.getParentIds().length);
	}

}
